<?php

/**
 * [PHPFOX_HEADER]
 */
defined('PHPFOX') or exit('NO DICE!');

/**
 * 
 * 
 * @copyright		[PHPFOX_COPYRIGHT]
 * @author  		Raymond Benc
 * @package 		Phpfox_Service
 * @version 		$Id: process.class.php 4977 2012-10-31 14:12:43Z Miguel_Espinoza $
 */
class Marketplace_Service_Store_Process extends Phpfox_Service {

    public function __construct() {
        $this->_sTable = Phpfox::getT("marketplace_store");
    }

    public function add($aVals) {

        $aSql = array(
            "name" => $aVals["store_name"],
            "country_iso" => isset($aVals["country_iso"]) ? $aVals["country_iso"] : null,
            "country_child_id" => isset($aVals["country_child_id"]) ? $aVals["country_child_id"] : null,
            "city" => isset($aVals["city"]) ? $aVals["city"] : null,
            "address" => isset($aVals["store_address"]) ? $aVals["store_address"] : null,
            "user_id" => Phpfox::getUserId(),
            "phone_number" => isset($aVals["phone_number"]) ? $aVals["phone_number"] : null
        );
        if (isset($aVals["store_link"]) && is_array($aVals["store_link"])) {
            $aSql["link_data"] = json_encode($aVals["store_link"]);
        }

        $iId = $this->database()->insert($this->_sTable, $aSql);

        return $iId;
    }
    public function update($iId,$aVals){
        $aSql = array(
            "name" => $aVals["store_name"],
            "country_iso" => isset($aVals["country_iso"]) ? $aVals["country_iso"] : null,
            "country_child_id" => isset($aVals["country_child_id"]) ? $aVals["country_child_id"] : null,
            "city" => isset($aVals["city"]) ? $aVals["city"] : null,
            "address" => isset($aVals["store_address"]) ? $aVals["store_address"] : null,
            "user_id" => Phpfox::getUserId(),
            "phone_number" => isset($aVals["phone_number"]) ? $aVals["phone_number"] : null
        );
        if (isset($aVals["store_link"]) && is_array($aVals["store_link"])) {
            $aSql["link_data"] = json_encode($aVals["store_link"]);
        }
        else{
            $aSql["link_data"] = null;
        }

        $this->database()->update($this->_sTable, $aSql,"store_id = ".$iId);

        return true;
    }

    public function updateTheme($aVals, $iId) {
        if (isset($aVals["theme_id"])) {
            $this->database()->update($this->_sTable, array('theme_id' => $aVals["theme_id"]), "store_id = " . $iId);
        }
        $oFile = Phpfox::getLib('file');
        $iFileSizes = 0;
        foreach ($_FILES['image']['error'] as $iKey => $sError) {
            if ($sError == UPLOAD_ERR_OK) {
                if ($aImage = $oFile->load('image[' . $iKey . ']', array(
                    'jpg',
                    'gif',
                    'png'
                        ), (Phpfox::getUserParam('marketplace.max_upload_size_listing') === 0 ? null : (Phpfox::getUserParam('marketplace.max_upload_size_listing') / 1024))
                        )
                ) {
                    $sFileName = Phpfox::getLib('file')->upload('image[' . $iKey . ']', Phpfox::getParam('marketplace.dir_image'), $iId);

                    $iFileSizes += filesize(Phpfox::getParam('marketplace.dir_image') . sprintf($sFileName, ''));
                }
            }
        }

        if ($iFileSizes === 0) {
            return false;
        }

        $this->database()->update($this->_sTable, array('image_path' => $sFileName, 'server_id' => Phpfox::getLib('request')->getServer('PHPFOX_SERVER_ID')), 'store_id = ' . $iId);
        // Update user space usage
        Phpfox::getService('user.space')->update(Phpfox::getUserId(), 'marketplace', $iFileSizes);
    }

}

?>